// pages/cart/index.js
const {
  getShopDel,
  getKillCarList,
  getShopNumList,
  getShop,
  getKillList
} = require("../../api/index")

Page({
  /**
   * 页面的初始数据
   */
  data: {
    shop: [], //总数据
    totalPrice: 0, //总价,
    CartList: [],
    arrList:[],
  },

  ////加加
  addShop(e) {
    // 点击加后将数量传到后台
    let numid = e.currentTarget.dataset.item.id //拿到每一项的id
    let numNum = e.currentTarget.dataset.item.count + 1 //点击数量+1
    //修改数量
    getShopNumList({
      numid,
      numNum
    }).then(res => {
      // console.log(res)
    })
    // 加加
    const index = e.currentTarget.dataset.index;
    let shop = this.data.shop;
    let count = shop[index].count;
    count = count + 1;
    shop[index].count = count;
    this.setData({
      shop: shop
    });
    //总价
    this.getTotalPrice();
  },

  //减减
  rentShop(e) {
    let numid = e.currentTarget.dataset.item.id //拿到每一项的id
    let numNum = e.currentTarget.dataset.item.count - 1 //点击数量+1
    if (numNum <= 1) {
      numNum = 1
    }
    getShopNumList({
      numid,
      numNum
    }).then(res => {
      // console.log(res)
    })
    // 拿到下标
    const index = e.currentTarget.dataset.index;
    let shop = this.data.shop;
    let num = shop[index].count;
    num = num - 1;
    if (num <= 1) {
      wx.showToast({
        title: '最少选一个',
        icon: '',
        duration: 1000,
        mask: true
      })
      num = 1
    }
    shop[index].count = num;
    this.setData({
      shop: shop
    });

    this.getTotalPrice();
  },
  //总价
  getTotalPrice(e) {
    let shop = this.data.shop;
    let total = 0;
    for (let i = 0; i < shop.length; i++) {
      if (shop[i].selected) {
        total += shop[i].count * shop[i].price;
      }
    }
    this.setData({
      shop: shop,
      totalPrice: total.toFixed(2),
    });
  },

  //单选
  selectList(e) {
    console.log(e, '22');
    const index = e.currentTarget.dataset.index;
    let shop = this.data.shop;

    const selected = shop[index].selected;
    shop[index].selected = !selected;
    this.setData({
      shop: shop
    });

    for(let i=0;i<this.data.shop.length;i++){
      if(this.data.selectAllStatus){
        if(!this.data.shop[i].selected){
          this.setData({
            selectAllStatus:false
          })
          break;
        }
      }else{
        if(this.data.shop[i].selected){
          this.setData({
            selectAllStatus:true
          })
        }else{
          this.setData({
            selectAllStatus:false
          })
          break;
        }
      }
    }

    //获取总价
    this.getTotalPrice();
  },

  // 结算
  settle(){
    let shop=this.data.shop
    let arritem=[]

    arritem=shop.filter(item=>{
      return item.selected===true
    })
  },

  //全选
  selectAll(e) {
    let selectAllStatus = this.data.selectAllStatus;
    selectAllStatus = !selectAllStatus;
    let shop = this.data.shop;
    for (let i = 0; i < shop.length; i++) {
      shop[i].selected = selectAllStatus;
    }
    this.setData({
      selectAllStatus: selectAllStatus,
      shop: shop
    });
    this.getTotalPrice();
  },
  //删除
  delShop(e) {
    let id = e.currentTarget.dataset.id
    getShopDel({
      id
    }).then(res => {
      // 删除成功 刷新购物车
      this.onLoad()
      wx.showToast({
        title: '删除成功',
        icon: 'success',
        duration: 1000,
        mask: true
      })
    })
    this.onLoad()
  },
  //
  //跳转结算页面
  settle:function(e){
    let shop=this.data.shop
    let arritem=[]
    arritem=shop.filter(item=>{
      return item.selected===true
    })
    let arrList=JSON.stringify(arritem);

    let addtotalPrice=this.data.totalPrice
    wx.navigateTo({
      url:"/pages/settle/index?arrList="+encodeURIComponent(arrList)+'&priceadd='+addtotalPrice,
    })
    this.onLoad()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 购物车列表
    getKillCarList({
      userId: wx.getStorageSync('openid'),
    }).then(res => {
      this.setData({
        shop: res.data.data
      })
    })
    getKillList({
      shopclass: 2,
    }).then(res => {
      this.setData({
        CartList: res.data.data.res
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //调用函数、方法   页面自动刷新
    this.onLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})